using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    public class AudioPlayer : MonoBehaviour
    {
        public AudioSource AudioSource { get; private set; }
        public bool IsPlaying { get; private set; }
        public float PlayingTime { get; private set; }
        public AudioTrackInfo Track { get;private set; }
        public Action<AudioPlayer> PlayerUpdate;
        public void Init(AudioTrackInfo track)
        {
            AudioSource = GetComponent<AudioSource>();
            AudioSource.loop = false;
            Track = track;
        }

        public void PlayAudio(AudioClip audioClip, float volume)
        {
            if (audioClip == null)
            {
                Debug.LogError("player can not play null audioClip");
                return;
            }
            IsPlaying = true;
            PlayingTime = 0;

            AudioSource.clip = audioClip;
            AudioSource.volume = volume;
            AudioSource.Play();
        }
        public void Stop()
        {
            AudioSource.Stop();
            IsPlaying = false;
        }
        private void Update()
        {
            if (!IsPlaying) return;
            PlayingTime += Time.deltaTime;
            PlayerUpdate?.Invoke(this);
        }
    }
}